from django.test import TestCase

# Create your tests here.
import requests


def my_log(func):
    def wrapper(*args,**kw):
        print("=====start %s=================" % func.__name__)
        return func(*args,**kw)
    return wrapper


base_url = "http://127.0.0.1:8000/"
class Test_login(object):
    def __init__(self,url):
        self.base_url = url
        print(self.base_url)

    @my_log
    def test_login(self):
        url = self.base_url + "login/"
        data = {'username':"admin","password":"123456"}
        resp = requests.post(url=url,data=data)
        # print(resp.json())
        self.token = resp.json()['data']['token']
        # print(self.token)

    @my_log
    def test_get_users(self):
        url = self.base_url + "users/"
        header ={"Authorization": "Bearer "+self.token}
        resp = requests.get(url=url,headers=header)
        print(resp.json())

    @my_log
    def test_get_groups(self):
        url = self.base_url + "groups/"
        header ={"Authorization": "Bearer "+self.token}
        resp = requests.get(url=url,headers=header)
        print(resp.json())

    @my_log
    def test_get_user(self):
        url = self.base_url + "user/"
        header ={"Authorization": "Bearer "+self.token}
        resp = requests.get(url=url,headers=header)
        print(resp.json())

    @my_log
    def test_get_cart(self):
        url = self.base_url + "carts/"
        header ={"Authorization": "Bearer "+self.token}
        resp = requests.get(url=url,headers=header)
        print(resp.json())

    @my_log
    def test_get_products(self):
        url = self.base_url + "products/"
        data = {
            'category':"1",
            'ordering':'sales',
            'page_size':"5",
            'page': "2"
        }
        header ={"Authorization": "Bearer "+self.token}

        resp = requests.get(url=url,headers=header,json=data)
        # print(resp.content)
        print(resp.json())

    @my_log
    def test_create_items(self):
        url = self.base_url + "items/"
        data = {
            'customer': '3',
            'product': '1',
            'count': '100',
            'selected': 'True',
            'cart': '2',
            'order':'',
        }
        header = {"Authorization": "Bearer "+self.token}
        resp = requests.post(url=url, headers=header, json=data)
        print(resp.json())

    @my_log
    def test_save_order(self):
        url = self.base_url + "orders/"
        data ={
            'customer':'2',
            'address':'1',
            'express':'1',
            'express_cost':12,
            'total':1200,
            'pay_method':1,
        }
        header = {"Authorization": "Bearer "+self.token}
        resp = requests.post(url=url, headers=header, json=data)
        print(resp.json())

if __name__ == "__main__":
    t=Test_login(base_url)
    t.test_login()
    # t.test_get_users()
    # # t.test_get_groups()
    # # t.test_get_user()
    # t.test_get_cart()
    # t.test_get_products()
    # t.test_create_items()
    t.test_save_order()